package com.example.smartcity.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import androidx.appcompat.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.smartcity.R;
import com.example.smartcity.activity.AdviseActivity;
import com.example.smartcity.activity.LoginActivity;
import com.example.smartcity.activity.NewsWebViewActivity;
import com.example.smartcity.activity.OrderActivity;
import com.example.smartcity.activity.RegisterActivity;
import com.example.smartcity.activity.UpdataPSWActivity;
import com.example.smartcity.activity.UserInfoActivity;
import com.example.smartcity.bean.NewsBean;
import com.example.smartcity.utils.APIConfig;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class UserFragment extends BaseFragment implements View.OnClickListener {
    private static final String TAG = UserFragment.class.getSimpleName();
    private TextView tv_username;
    private ImageButton user_img;
    private Button btn_out;
    private Intent intent = null;
    private LinearLayout ll_info;
    private LinearLayout ll_order;
    private LinearLayout ll_update;
    private LinearLayout ll_advice;
    private Toolbar toolbar;
    private TextView psw_upd;
    private TextView tv_info;
    private int uid;
    private ImageButton ib_update_psw;
    private String result;
    private TextView tv_sc;



    @Override
    public View initView() {
        Log.i(TAG, "用户中心的视图被实例化了");
        View view = View.inflate(getContext(), R.layout.fragment_user, null);
        tv_info = view.findViewById(R.id.tv_info);
        psw_upd = view.findViewById(R.id.psw_upd);
        ll_advice = view.findViewById(R.id.ll_advice);
        ll_info = view.findViewById(R.id.ll_info);
        ll_order = view.findViewById(R.id.ll_order);
        ll_update = view.findViewById(R.id.ll_update);
        btn_out = view.findViewById(R.id.btn_out);
        tv_username = view.findViewById(R.id.tv_username);
        user_img = view.findViewById(R.id.user_img);
        ib_update_psw = view.findViewById(R.id.ib_update_psw);
        tv_sc = view.findViewById(R.id.tv_sc);
        ll_update.setOnClickListener(this);
        ll_info.setOnClickListener(this);
        ll_order.setOnClickListener(this);
        ll_advice.setOnClickListener(this);
        btn_out.setOnClickListener(this);
        user_img.setOnClickListener(this);
        return view;
    }


    @Override
    public void initData() {
        super.initData();
        // 读取本地保存用户的登录信息
        SharedPreferences sp = getActivity().getSharedPreferences("token_data", Context.MODE_PRIVATE);
        String user = sp.getString("username","匿名用户");
        uid = sp.getInt("token",1);
        Log.i("uf.uid",uid+"");
        getUserAvatar(uid);


        Glide.with(getActivity()).load(result).into(ib_update_psw);

        if (uid == 1){
            btn_out.setText("去登录");
            tv_sc.setText("");
            psw_upd.setText("");
            tv_info.setText("去注册    ");
            ib_update_psw.setImageDrawable(null);
        }
        // 把用户名显示到textView控件上
        tv_username.setText(user);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_info:
                if(uid == 1){intent = new Intent(getContext(), RegisterActivity.class);}
                else{intent = new Intent(getContext(), UserInfoActivity.class);}
                break;
            case R.id.ll_order:
                if (uid== 1){intent = new Intent(getContext(), LoginActivity.class);}
                intent = new Intent(getContext(), OrderActivity.class);
                intent.putExtra("uid",uid);

                break;
            case R.id.ll_update:
                if(uid != 1){intent = new Intent(getContext(), UpdataPSWActivity.class);}
                else{intent = new Intent(getContext(), RegisterActivity.class);}

                break;
            case R.id.ll_advice:
                intent = new Intent(getContext(), AdviseActivity.class);
                break;
            case R.id.btn_out://清除用户在登录界面保存的id，以便再次登录
                SharedPreferences.Editor editor = getActivity().getSharedPreferences("token_data", Context.MODE_PRIVATE).edit();
                editor.clear(); // 清除本地保存的token值
                editor.apply();
                intent = new Intent(getContext(), LoginActivity.class);
                break;
            case R.id.user_img:
                if(uid == 1){intent = new Intent(getContext(), RegisterActivity.class);}
                else{intent = new Intent(getContext(), UserInfoActivity.class);}
                break;
        }
        // 页面跳转
        startActivity(intent);

    }
    private void getUserAvatar(int uid){
        Log.i("getUserAvatar"," Activated getAvatar");
        OkHttpClient client = new OkHttpClient();
        Request request  = new Request.Builder()
                .url(APIConfig.BASE_URL+"/user/getavatar?uid="+uid)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("onFailure",e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    result = response.body().string();
                    Log.i("getUserAvatarresult",result);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 在这里调用Glide的方法
                            int cornerRadius = getResources().getDimensionPixelSize(R.dimen.corner_radius); // 获取圆角半径的值
                            //定义圆形头像
                            Glide.with(getActivity()) // 使用Glide加载图片
                                    .load(APIConfig.BASE_URL+result) // 加载图片的URL
                                    .apply(new RequestOptions() // 应用请求选项
                                            .transform(new RoundedCorners(cornerRadius))) // 设置图片的圆角半径
                                    .into(user_img); // 将图片设置到ImageView中
                        }
                    });

                }
            }
        });
    }
}
